package net.dodoman.excel;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelReader {

	private final static String EXCEL_PATH = "E:/giswork/features.xls";

	private final static int FEATURE_NUMBERS = 39;

	public static void main(String[] args) throws ReadException {
		try {
			BatWriter.generateBat(ExcelReader.read(EXCEL_PATH));
		} catch (IOException e) {
			throw new ReadException("IO异常", e);
		} catch (ReadException e1) {
			throw new ReadException("读取行列异常", e1);
		}
	}

	public final static List<String> read(String path) throws IOException,
			ReadException {
		// 返回List
		List<String> list = new ArrayList<String>();
		// 构造 HSSFWorkbook 对象，strPath 传入文件路径
		HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(path));
		// 读取文件中的第一张表格
		HSSFSheet sheet = wb.getSheetAt(0);
		// 定义 row、cell
		HSSFRow row;
		// HSSFCell cell;
		String cell;
		// 循环输出表格中的内容
		for (int i = sheet.getFirstRowNum(); i < FEATURE_NUMBERS; i++) {
			row = sheet.getRow(i);
			for (int j = row.getFirstCellNum(); j < row
					.getPhysicalNumberOfCells(); j++) {
				// 推荐通过 row.getCell(j).toString() 获取单元格内容，
				cell = row.getCell(j).toString();
				list.add(cell);
				// System.out.print(cell+"\n");
			}
		}
		return list;
	}
}
